<?php
namespace App\Models;
use Log;
use App\Cache\BaseCache;
use DB;

class Gift extends BaseModel {
    //表明
    protected $table = 'gift';
    //主键
    protected $primaryKey = 'id';

    //用户实体信息
    protected function getGiftObejct($id, $giftModel = null) {
        $gift = $this->getGiftModel($id, $giftModel);
        if (isset($gift->id) && $gift->is_delete != 1) {
            $giftObject = array();
            $giftObject['giftId'] = $gift->id;
            $giftObject['giftName'] = $gift->name; //token
            $giftObject['giftImg'] = $gift->img;
            $giftObject['giftMiddleImg'] = $gift->middle_img;
            $giftObject['giftUnit'] = $gift->unit;
            $giftObject['gold'] = $gift->gold;
            return $giftObject;
        }
        return;
    }

    //获取礼物实体
    public function getGiftModel($id, $giftModel = '') {
        $modelCache = new BaseCache();
        $modelCache->method = 'get';
        $modelCache->cacheName = 'giftModel';
        $modelCache->cachePar = array($id); //集合
        $retCache = $modelCache->HandleCache();
        if ($retCache !== 'NO_CACHE') {
            if (!isset($retCache->id)) {
                return;
            }
            return $retCache;
        }
        if (isset($giftModel->id) && $giftModel->id > 0) {
            $gift = $giftModel;
        } else if ($id > 0) {
            $gift = $this->select('id','name','img','middle_img','unit','gold','is_delete')
                    ->where('id', '=', $id)
                    ->first();
        } else {
            $gift = 'CACHE_BREAKDOWN';
        }
        $modelCache->method = 'put';
        $modelCache->cacheValue = $gift;
        $modelCache->HandleCache();
        if (!isset($gift->id)) {
            return;
        }
        return $gift;
    }

    //获取礼物列表
    public function getGiftList() {
        $modelCache = new BaseCache();
        $modelCache->method = 'get';
        $modelCache->cacheName = 'giftList';
        $retCache = $modelCache->HandleCache();
        if ($retCache === 'CACHE_BREAKDOWN') {
            return array();
        }
        $data = array();
        if (is_array($retCache) && count($retCache) > 0) {
            $gifts = $retCache;
            foreach ($gifts as $gift) {
                $index = $this->getGiftObejct($gift['id']);
                if (isset($index['giftId'])) {
                    $data[] = $index;
                }
            }
        } else {
            $gifts = $this->select('id', 'name', 'img','middle_img','unit','gold', 'is_delete')
                    ->where('is_delete', '=', 0)
                    ->orderBy('sort', 'asc')
                    ->take(6)
                    ->get();
            foreach ($gifts as $gift) {
                $index = $this->getGiftObejct($gift->id, $gift);
                if (isset($index['giftId'])) {
                    $modelCache->cacheValue[] = array('id' => $index['giftId']);
                    $data[] = $index;
                }
            }
            $modelCache->method = 'put';
            $modelCache->HandleCache();
        }
        return $data;
    }
    
    //送礼物
    public function sendGift() {
        $userId = $this->request->input('userId');
        $liveId = $this->request->input('liveId');
        $giftId = $this->request->input('giftId');
        $gift = $this->getGiftModel((int)$giftId);
        $live = with(new Live($this->request))->getLiveModel($liveId);
        $user = with(new User($this->request))->getUserModel($userId);
        if(!isset($gift->id) || !isset($live->id) || !isset($user->id)){
//            Log::info('sendGift 1 giftId'.$giftId);
            return;
        }
        if($user->gold - $gift->gold < 0){
//            Log::info('sendGift 2');
            return;
        }
        //事务
        DB::connection(env('MYSQL_CONNECTION','mysql'))->beginTransaction();
        try {
            //送礼物记录
            $giftRecord = new GiftRecord();
            $giftRecord->gift_id = $gift->id;
            $giftRecord->live_id = $live->id;
            $giftRecord->user_id = $user->id;
            $giftRecord->gift_gold = $gift->gold;
            $giftRecord->gift_time = time();
            if(!$giftRecord->save()){
                Log::info('sendGift 3');
                return;
            }
            $user->gold = $user->gold - $gift->gold;      
            $user->save();
        } catch (\Exception $exc) {
//            Log::info('sendGift 4');
            DB::connection(env('MYSQL_CONNECTION','mysql'))->rollBack();
            return ;
        }
        DB::connection(env('MYSQL_CONNECTION','mysql'))->commit();
        return array('gold'=>$user->gold);
    }
    

}
